package com.yicong.ants.manager;

import android.app.Activity;
import android.content.ClipboardManager;
import android.content.Context;

import com.cchao.simplelib.LibCore;
import com.cchao.simplelib.core.AndroidHelper;
import com.cchao.simplelib.core.Logs;
import com.cchao.simplelib.core.PrefHelper;
import com.cchao.simplelib.core.RxBus;
import com.cchao.simplelib.util.StringHelper;
import com.yicong.ants.Constants;
import com.yicong.ants.manager.app.AntActivityListener;
import com.yicong.ants.manager.business.Dialogs;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 剪切板管理
 *
 * @author cch
 * @version 2020/12/29
 */
public class CopyHelper {
    public static ClipboardManager.OnPrimaryClipChangedListener mClipChangedListener;

    /**
     * 提取ants协议
     */
    public static String extractClipAnts() {
        String content = AndroidHelper.getClipboardShareData();
        if (!RouterHelper.isAntsLink(content)) {
            return "";
        }
        if (content.contains("ants://")) {
            return content.substring(content.indexOf("ants://"));
        }

        if (content.contains("ants_link=1")) {
            try {
                String regex = "\\w+://\\S+";
                Pattern pattern = Pattern.compile(regex);
                Matcher m = pattern.matcher(content);
                while(m.find()){
                    return  m.group();
                }
            } catch (Exception exception) {
                return "";
            }
        }
        return "";
    }


    /**
     * 监听剪切板
     */
    public static void listenClip() {
        ClipboardManager cb = (ClipboardManager) LibCore.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
        cb.removePrimaryClipChangedListener(mClipChangedListener);
        cb.addPrimaryClipChangedListener(mClipChangedListener = () -> {
            if (RouterHelper.isAntsLink(AndroidHelper.getClipboardShareData())) {
                String antLink = extractClipAnts();
                // 在前台就不弹出 蚁丛口令提示
                if (AntActivityListener.isAppVisible()) {
                    PrefHelper.putString(Constants.Prefs.Clipboard_links_routed_before, antLink);
                }
                RxBus.get().postEvent(Constants.Event.Ant_Link_Copy, antLink);
            }
        });
    }

    /**
     * 路由
     *
     * @return 是否路由成功
     */
    public static boolean router(Activity context) {
        String antUrl = extractClipAnts();
        if (StringHelper.isEmpty(antUrl)) {
            return false;
        }

        Logs.logEvent("CopyHelper.router url " + antUrl);

        if (antUrl.equalsIgnoreCase(PrefHelper.getString(Constants.Prefs.Clipboard_links_routed_before, ""))) {
            return false;
        }

        Dialogs.showAntLinkRouter(context, antUrl, () -> {
            // ants://platform/app?id=11040&uid=1023456
            RouterHelper.tryRoutePlatformUri(context, antUrl);
        });

        PrefHelper.putString(Constants.Prefs.Clipboard_links_routed_before, antUrl);
        return true;
    }
}
